
require "framework.init"

local lumen = z9.lumen
local sched = lumen.sched
local mutex = lumen.mutex

local mx = mutex.new() --创建一个mutex

local function func(n)
    print('+' .. n)
    sched.sleep(1)
    print('', '-'..n)
end

local critical = mx:synchronize(func)

print "非同步方式"
sched.run(function()
    for i=1, 5 do
        func('A')
        sched.wait()
    end
end)
sched.run(function()
    for i=1, 5 do
        func('B')
        sched.wait()
    end
end)
sched.loop()


print("同步方式") --就是加了一个互斥锁
sched.run(function()
    for i=1, 5 do
        critical('A')
        sched.wait()
    end
end)
sched.run(function()
    for i=1, 5 do
        critical('B')
        sched.wait()
    end
end)
sched.loop()
